package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Vector;
import javax.swing.DefaultRowSorter;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.border.LineBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import CommonGui.NotEdditibleTable;
import DataObjects.ConstantObjects;
import Messages.MessageSavingReply;

public class PfSearch_supplier extends JPanel  {
	private NotEdditibleTable dtm;              //extends DefaultTableModel
	private MainMenu mainMenu;
	private Vector<Vector> allSupp;            // each supplier is ordered by suppID,suppName,suppPhoneNo,suppPhoneNo2,suppCellNo,suppMail,suppFax,suppContactMen,suppZipCode,suppCity,suppHouse,suppStreet
	private JTable tbl;
	private VKTextField nameTF;
	private VKTextField lastNameTF;
	private RowSorter<DefaultTableModel> sorterRole;
	private RowSorter<DefaultTableModel> sorterName;
	private RowSorter<DefaultTableModel> sorterLastName;
	
	public PfSearch_supplier(MainMenu mm, Vector<Vector> supp)
	{
		super();
		this.setLayout(new BorderLayout());
		this.mainMenu=mm;
		this.allSupp=supp;
		if (supp.size()!=0)
		initiliaze();
	}	
	
	private void initiliaze()
	{
		JScrollPane scrollPane = new JScrollPane(getTblSupp());
		this.add(scrollPane,BorderLayout.CENTER);
		this.add(getSearchUnits(),BorderLayout.NORTH);
	}
	
	// each supplier is ordered by suppID,suppName,suppPhoneNo,suppPhoneNo2,suppCellNo,suppMail,suppFax,suppContactMen,suppZipCode,suppCity,suppHouse,suppStreet
	private JTable getTblSupp()
	{
		Vector <String> tblheader=new Vector<String>();
		tblheader.add("ID");
		tblheader.add("שם ספק");
		tblheader.add("מספר טלפון");
		tblheader.add("מספר טלפון2");
		tblheader.add("מספר טלפון סלולרי");
		tblheader.add("E-mail");
		tblheader.add("מספר פקס");
		tblheader.add("שם איש קשר");
		tblheader.add("8");
		tblheader.add("9");
		tblheader.add("10");
	
		
		
		dtm=new NotEdditibleTable(allSupp,tblheader);	 		
		tbl = new JTable(dtm);
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<8;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
		
		
		tbl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		
		  tbl.removeColumn(tbl.getColumnModel().getColumn(10));
		  tbl.removeColumn(tbl.getColumnModel().getColumn(9));
		  tbl.removeColumn(tbl.getColumnModel().getColumn(8));
		  
		
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
	    tbl.setRowHeight(30);
	
		tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(250, 235, 215));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
        
        
        
        tbl.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			     if (e.getClickCount() == 1) {
			    	 
			       JTable target = (JTable)e.getSource();
			       int row = target.getSelectedRow();
			       int column = target.getSelectedColumn();
			       // do some action if appropriate column
			       if (row!=-1)
			       {
				       mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));			       			       
				       Vector<String> vec=(Vector<String>) dtm.getDataVector().get(row);			    
				       mainMenu.getContentPane().add(new PanelFunctionSupplierThis(mainMenu,vec), BorderLayout.EAST);
				       mainMenu.getContentPane().validate();			    	 
			       }// if (row!=-1)
			     }// if (e.getClickCount() == 1)
			   }//public void mouseClicked(MouseEvent e)
			 });
        
        
        return tbl;
	}
	
	
	private JPanel getSearchUnits()
	{
		JPanel searchUnits=new JPanel ();
		searchUnits.setBackground(new Color(255, 245, 238));
		searchUnits.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		searchUnits.add(getNameTF());
		searchUnits.add(getNameLbl());
		searchUnits.add(getLastNameTF());
		searchUnits.add(getLastNameLbl());
		
		return searchUnits;
	}

	
	private VKTextField getNameTF()
	{
		nameTF=new VKTextField(mainMenu);
		nameTF.setHorizontalAlignment(JTextField.RIGHT);
		nameTF.setColumns(10);
		nameTF.setMaximumSize( nameTF.getPreferredSize() );
		nameTF.setMinimumSize( nameTF.getPreferredSize() );
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				VKTextField nameField = (VKTextField)arg0.getSource();
				String selectedName = (String)nameField.getText();
				filterByName(selectedName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		nameTF.addKeyListener(keyListener);		
		return nameTF;
	}
	
	private void filterByName(String name)
	{
		
		sorterName = new TableRowSorter<DefaultTableModel>(dtm);     		   
	    tbl.setRowSorter(sorterName);  	    
	    RowFilter< DefaultTableModel  , Object> rf = null;   
	    //declare a row filter for your table model  
	    try  
	    {  
	        rf = RowFilter.regexFilter(name, 1);    
	        //initialize with a regular expression  
	    }  
	   catch (java.util.regex.PatternSyntaxException e1)  
	    {  
	        return;  
	    }  
	   
	    ((DefaultRowSorter<DefaultTableModel, Integer>) sorterName).setRowFilter(rf);		
	}
	
	private VKTextField getLastNameTF()
	{
	    lastNameTF=new VKTextField(mainMenu);
	    lastNameTF.setHorizontalAlignment(JTextField.RIGHT);
	    lastNameTF.setColumns(10);
	    lastNameTF.setMaximumSize( lastNameTF.getPreferredSize() );
	    lastNameTF.setMinimumSize( lastNameTF.getPreferredSize() );
	    KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				// TODO Auto-generated method stub
				VKTextField lastNameField = (VKTextField)arg0.getSource();
				String selectedLastName = (String)lastNameField.getText();
				filterByLastName(selectedLastName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		lastNameTF.addKeyListener(keyListener);		
		
		return lastNameTF;
	}
	
	private void filterByLastName(String lastName)
	{
		
		sorterLastName = new TableRowSorter<DefaultTableModel>(dtm);     		   
	    tbl.setRowSorter(sorterLastName);  	    
	    RowFilter< DefaultTableModel  , Object> rf = null;   
	    //declare a row filter for your table model  
	    try  
	    {  
	        rf = RowFilter.regexFilter(lastName, 2);    
	        //initialize with a regular expression  
	    }  
	   catch (java.util.regex.PatternSyntaxException e1)  
	    {  
	        return;  
	    }  
	   
	    ((DefaultRowSorter<DefaultTableModel, Integer>) sorterLastName).setRowFilter(rf);		
	}
	
	
	private JLabel getNameLbl()
	{
		JLabel nameLbl=new JLabel("  שם :  ");
		nameLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		return nameLbl;
	}
	
	private JLabel getLastNameLbl()
	{
		JLabel lastNameLbl=new JLabel("   איש קשר  :  ");
		lastNameLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		return lastNameLbl;
	}


	
	
	
}
	


